package com.lc.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;


/**
 * Created by lichao on 2017/6/1.
 */
    /**
     *
     * <p class="detail">
     * 功能：自定义bean扫描
     * </p>
     * @ClassName: KingBeanScannerConfigurer
     * @version V1.0
     * @date 2014年12月17日
     * @author <a href="mailto:wangs@financegt.com">kings</a>
     */
    public class KingBeanScannerConfigurer implements BeanDefinitionRegistryPostProcessor {
        /**
         * ,; \t\n
         */
        private Class<?> kInterface;
        private String basePackage;

        public String getBasePackage() {
            return basePackage;
        }


        public void setkInterface(Class<?> kInterface) {
            this.kInterface = kInterface;
        }


        @Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
                throws BeansException {
            registerRequestProxyHandler(registry);
            KingClassPathMapperScanner scanner = new KingClassPathMapperScanner(registry,kInterface);
            scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage,
                    ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
        }

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
                throws BeansException {
        }

        /**
         * RequestProxyHandler 手工注册代理类,减去了用户配置XML的烦恼
         *
         * @param registry
         */
        private void registerRequestProxyHandler(BeanDefinitionRegistry registry) {
            GenericBeanDefinition requestProxyDefinition = new GenericBeanDefinition();
            requestProxyDefinition.setBeanClass(KingProxy.class);
            registry.registerBeanDefinition("kingProxy", requestProxyDefinition);
        }

        public void setBasePackage(String basePackage) {
            this.basePackage = basePackage;
        }
    }
